
// lib
// import moment from "moment";
// config
// script & methods & public
import toast from "components/toast";
// import timeUtils from "utils/timeUtils";
// import { getUriArrFromUoloadFile } from "biz/File/logic";
// store
import { state } from "./state";
// service
import {
  addAdminService,
  delAdminService,
  editAdminStateService,
  getAdminPageService,
  resetAdminPwdService,
  updateAdminService,
} from "biz/Admin/service";
import { getRoleListService } from "biz/Role/service";
// interface
import { ReqAddAdmin, ReqAdminUpdate, ReqAdminPage } from "biz/Admin/api";

export async function getAdminPage() {
  const sp = state.getSearch();
  const params: ReqAdminPage = {
    pageNum: state.page,
    pageSize: state.size,
    ...sp
  };
  state.loading = true;
  const result = await getAdminPageService(params);
  state.loading = false;
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}

export async function addAdmin(form: any) {
  const params: ReqAddAdmin = {
    ...form,
  };
  state.editModalLoading = true;
  const result = await addAdminService(params);
  state.editModalLoading = false;
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getAdminPage();
  }
}

export async function updateAdmin(form: any, userId: number) {
  // const avatar = getUriArrFromUoloadFile(form.avatar)[0];
  const params: ReqAdminUpdate = {
    ...form,
    userId,
  };
  state.editModalLoading = true;
  const result = await updateAdminService(params);
  state.editModalLoading = false;
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getAdminPage();
  }
}

export async function delAdmin(id: number) {
  const result = await delAdminService(id);
  if (result.payload) {
    toast.success("删除成功");
    state.setSearch();
    getAdminPage();
  }
}

export async function resetAdminPwd(pass: string, userId: number) {
  state.pwdModalLoading = true;
  const result = await resetAdminPwdService({ pass, userId });
  state.pwdModalLoading = false;
  if (result.payload) {
    toast.success("重置成功");
    state.pwdModal = false;
  }
}

export async function changeAdminState(newState: number, userId: number) {
  const result = await editAdminStateService({ state: newState, userId });
  if (result.payload) {
    toast.success("修改成功");
    state.setSearch();
    getAdminPage();
  }
}

export async function didMount() {
  getRoleListService();
  getAdminPage();
}
